<?php
namespace App\Console\Commands;
use App\Common\Ai\XFYunApi;
use App\Common\Excel\ExcelSpout;
use App\Common\Logs\Logs;
use App\Common\Utill\Utills;
use App\Models\CategoryImport;
use App\Models\WebsiteCrontab;
use App\Models\Websites;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Test extends Command
{
    /**
     * php artisan Test
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        //$this->websiteCopy();
        //$this->websiteAd();
//        $this->saveImg();
//        exit();
        //$this->websiteCrontab();
        //$this->websiteData();
        //$this->categoryData();
        //$this->categoryImport();
        Logs::crontabLog("test 已运行");
    }

    public function websiteCopy()
    {
        $data = DB::table("pd_websites_copy")->get()->toArray();
        foreach ($data as $kk=>$vv){
             $postData =[];
             $postData['names']=$vv->names;
             $postData['domains']=$vv->domains;
             $postData['created_at']=date('Y-m-d H:i:s');
             DB::table("pd_websites")->insert($postData);
             echo "id:".$vv->id.PHP_EOL;
        }

    }


    public function websiteAd()
    {
        $data = DB::table("pd_website_ad")
            ->where("website_id", "<>",1)
            ->get()
            ->toArray();
        foreach ($data as $val){
            $saveData =[];
            if($val->styles == 1){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/shu1.png";
            }elseif ($val->styles == 2){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/heng2.png";
            }elseif ($val->styles == 3){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/da2.png";
            }
            DB::table("pd_website_ad")->where("id",$val->id)->update($saveData);
            echo "id:".$val->id."完成".PHP_EOL;
        }
    }


    public function promotion()
    {
        $data = DB::table("pd_promotion")
            ->where("id", ">",1)
            ->select("id", "url_address")
            ->get()
            ->toArray();
        $newData = [];
        $i=0;
        foreach ($data as $val){
            $newData[$i]['website_id']=$val->id-1;
            $newData[$i]['thumbs']= "https://qinipic.pindarpr.com/uploads/thumbs/ac1.png";
            $newData[$i]['jump_url']=$val->url_address;
            $newData[$i]['styles'] = 3;
            $newData[$i]['created_at']=date("Y-m-d H:i:s");
            $i++;
        }
        DB::table("pd_website_ad")->insert($newData);
        echo "<pre>";
        print_r($newData);
        echo "</pre>";
        exit();

    }

    public function saveImg()
    {
        $data = DB::table("pd_banners")->where("id", ">", 3)->get()->toArray();
        foreach ($data as $val){
            $saveData =[];
            if($val->sort == 1){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/lun11.png";
            }elseif ($val->sort == 2){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/lun14.png";
            }elseif ($val->sort == 3){
                $saveData['thumbs'] = "https://qinipic.pindarpr.com/uploads/thumbs/lun15.png";
            }
            $saveData['jump_url'] = DB::table("pd_website_ad")->where("website_id", $val->website_id)->value("jump_url");
            DB::table("pd_banners")->where("id",$val->id)->update($saveData);
            echo "id:".$val->id."完成<br>";
        }
    }

    public function websiteCrontab()
    {
        $j=0;
        $addData = [];
        for ($i = 1; $i <= 162; $i++) {
            $addData[$j]['website_id'] = $i;
            $addData[$j]['status'] = 1;
            $addData[$j]['created_at'] = date('Y-m-d H:i:s');
            $j++;
        }
        WebsiteCrontab::query()->insert($addData);
        exit();
    }

    public function websiteData()
    {
        $categroy = CategoryImport::query()->get()->toArray();
        $categroyId = array_column($categroy,"category_name","id");
        for ($i=145; $i <= 162; $i++) {
            echo "网站id:".$i."\n";
            // 获取三个随机索引
            $keys = array_rand($categroyId, 3);
            // 根据这些索引获取值
            $randomNumbers = array_intersect_key($categroyId, array_flip($keys));
            $randomNumbers = array_values($randomNumbers);
            Logs::crontabLog("num1:{$randomNumbers[0]},num2:{$randomNumbers[1]},num3:{$randomNumbers[2]}");
            $orderSn = Utills::aiOrderSn();
            $getTitleText = "{$randomNumbers[0]},{$randomNumbers[1]},{$randomNumbers[2]} 根据3个词生成 科技方面 seo的title  ,keywords , description。title里面的内容含有3个关键词，每个关键词用 - 分隔，再加一些内容，使其更加联顺。返回json格式一维数组。";
            $xfYunApi = new XFYunApi();
            $aiResult = $xfYunApi->sendRequest($orderSn, $getTitleText);
            $content = trim($aiResult['content'], "```json \n");
            $answer = json_decode($content, true);
            $saveData = [];
            $saveData['titles'] =  $answer[0]['title'] ?? $answer[0];
            $saveData['keywords'] =  $answer[0]['keywords'] ?? $answer[1];
            $saveData['description'] = $answer[0]['description'] ?? $answer[2];
            DB::table("pd_websites")->where("id",$i)->update($saveData);
        }
        echo "finished:".date("Y-m-d H:i:s");
    }


    public function categoryData()
    {
        $websiteData = Websites::query()->select("id")->get()->toArray();
        $i = 0;
        $categoryImport = CategoryImport::query()->get()->toArray();
        $categoryData = array();
        foreach ($websiteData as $key=>$val){
            foreach ($categoryImport as $kk=>$vv){
                $categoryData[$i]['admin_id'] = 1;
                $categoryData[$i]['website_id'] = $val['id'];
                $categoryData[$i]['category_name'] = $vv['category_name'];
                $categoryData[$i]['type'] = 1;
                $categoryData[$i]['use_numbers'] = 0;
                $categoryData[$i]['is_del'] = 1;
                $categoryData[$i]['operation_id'] = 1;
                $categoryData[$i]['weights'] = 1;
                $categoryData[$i]['created_at'] = date("Y-m-d H:i:s");
                $i++;
            }
        }
        $newData = array_chunk($categoryData,100);
        foreach ($newData as $news){
            DB::table("pd_category")->insert($news);
        }
        echo "finished:".date('Y-m-d H:i:s');

    }

    public function categoryImport()
    {
        $filePath = public_path("uploads/excel/category20251118.xlsx");
        $box = new ExcelSpout();
        $data = $box->import($filePath);
        $newData = [];
        $i = 0;
        foreach ($data as $key=>$val){
            if($key > 0 ){
                $newData[$i]['category_name'] = $val[0];
                $newData[$i]['weights'] = $val[1];
                $newData[$i]['created_at'] = date("Y-m-d H:i:s");
                $i++;
            }
        }
        DB::table("pd_category_import")->insert($newData);
        echo "<pre>";
        print_r($newData);
        echo "</pre>";
        exit();
    }
}
